package com.tang.service.impl;

import com.tang.config.BaseContext;
import com.tang.dao.TakeFoodDao;
import com.tang.entity.Food;
import com.tang.entity.TakeFood;
import com.tang.mapper.FoodMapper;
import com.tang.mapper.TakeFoodMapper;
import com.tang.pojo.Result;
import com.tang.pojo.TypedishVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.tang.service.TakeFoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 服务接口实现
 *
 * @author kancy
 * @since 2024-07-21 14:12:37
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TakeFoodServiceImpl implements TakeFoodService {
    private final TakeFoodDao takeFoodDao;

    @Autowired
    private TakeFoodMapper takeFoodMapper;

    @Autowired
    private FoodMapper foodMapper;



    public Result addFood(Long foodId, Double number, Integer type) {
        TakeFood takeFood=new TakeFood();
        takeFood.setFoodId(foodId);
        takeFood.setCount(number);
        takeFood.setType(type);
        takeFood.setTime(new Date());
        takeFood.setUserId(BaseContext.getCurrentId());
        takeFoodDao.save(takeFood);
        return Result.success();

    }

    @Override
    public Result<TypedishVo> getTypedish() {
        List<Food> br=new ArrayList<>();
        List<Food> lu=new ArrayList<>();
        List<Food> di=new ArrayList<>();
        List<Food> ot=new ArrayList<>();

        //获取用户id
        Long userId=BaseContext.getCurrentId();
        //获取当前localdatatime
        LocalDate now=LocalDate.now();
        Date date = new Date();
        log.info(now.toString());
        log.info(date.toString());
        //查表获取takefood
        List<TakeFood> foodListByUserIdAndTIme = takeFoodMapper.getFoodListByUserIdAndTIme(userId, now.toString());
        //获取foodid
        foodListByUserIdAndTIme.forEach(takeFood -> {
            //查找foodid  计算takefood中的数量 乘以 当前 食物的热量
            Long foodId=takeFood.getFoodId();
            Food foodById = foodMapper.getById(foodId);
            Double count=takeFood.getCount()/100f;
            foodById.setCalories(foodById.getCalories()*count);
            foodById.setFat(foodById.getFat()*count);
            foodById.setSugar(foodById.getSugar()*count);
            foodById.setProtein(foodById.getProtein()*count);
            switch (takeFood.getType()) {
                case 0:
                    br.add(foodById);
                    break;
                case 1:
                    lu.add(foodById);
                    break;
                case 2:
                    di.add(foodById);
                    break;
                case 3:
                    ot.add(foodById);
                    break;

            }


        });
        TypedishVo typedishVo=new TypedishVo();
        typedishVo.setBreakfast(br);
        typedishVo.setLunch(lu);
        typedishVo.setDinner(di);
        typedishVo.setOther(ot);
        return Result.success(typedishVo);

    }

}